import com.sun.speech.freetts.VoiceManager

module CcMonitor
  module BuildinActions
    def say(*something)
      something = something.flatten.join(" ").gsub(/\./, '. ').gsub(/"/, %{""})
      voiceManager = VoiceManager.getInstance()
      voice = voiceManager.getVoice('kevin16')
      begin
        voice.allocate
        voice.speak something
      ensure
        voice.deallocate
      end
    end
  
    def growl(*opt)
      balloon_messager(*opt)
    end
    
    def play(file)
      system 'C:\\Program Files\\Windows Media Player\\wmplayer.exe', File.expand_path(file)
    end
  
    def balloon_messager(*opt)
      title, desc, icon = opt.flatten
      TrayIconBalloonMessager.new(TRAY_ICON).display title, desc, icon
    end
  
    def method_missing(method, *args)
      if method.id2name =~ /^let_([^_]+)_say/ && args.length > 0
        say args[0]
      else
        super
      end
    end
  end
  
  
  class TrayIconBalloonMessager
  
    def initialize(ti)
      @ti = ti
    end
  
    def display(title, message, icon=nil)
      if icon.nil?
        if title =~ /failed/i
          icon = TrayIcon::WARNING_MESSAGE_TYPE
        elsif title =~ /loaded error/i
          icon = TrayIcon::ERROR_MESSAGE_TYPE
        else
          icon = TrayIcon::INFO_MESSAGE_TYPE
        end
      end
      message = ' ' unless message
      @ti.displayMessage(title.to_s, message.to_s, icon)
    end
  end
end
